package gui;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

public class Graphic {

	
	/**
	 * Rotates an image at a certain angle in degrees
	 * 
	 * @param img
	 *            |The Image to rotate
	 * @param angle
	 *            |The angle to rotate, counterclockwise
	 * @return |A copy of the image rotated;
	 */
	public static BufferedImage rotate(BufferedImage img, int angle) {
		angle = -angle;
		int w = img.getWidth();
		int h = img.getHeight();
		BufferedImage dimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = dimg.createGraphics();
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
		g.rotate(Math.toRadians(angle), w / 2, h / 2);

		g.drawImage(img, null, 0, 0);
		g.dispose();
		return dimg;
	}

	/**
	 * Scales an image with a given factor
	 * @param img
	 * 			|The image to scale
	 * @param factor
	 * 			|the scale
	 * @return
	 * 			| a new image with dimension factor*img
	 */
	public static BufferedImage scale(BufferedImage img, double factor) {

		int w = (int) (img.getWidth() * factor);
		int h = (int) (img.getHeight() * factor);
		BufferedImage dimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = dimg.createGraphics();
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g.scale(factor, factor);

		g.drawImage(img, null, 0, 0);
		g.dispose();
		return dimg;
	}
	
}
